home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14344 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  143 lines

  1. Path: topaz.cqu.edu.au!naderr
  2. From: naderr@topaz.cqu.edu.au
  3. Newsgroups: comp.lang.c++
  4. Subject: handling void
  5. Date: 30 Mar 96 07:31:51 +1000
  6. Organization: Central Queensland University, Australia
  7. Message-ID: <1996Mar30.073151@topaz>
  8. NNTP-Posting-Host: topaz.cqu.edu.au
  9.  
  10. Hi,
  11.  
  12. In a header file I have a class foo which has
  13. a public data member a pointer to a structure like ...
  14.  
  15. typedef class node_tag {
  16. public:
  17.   void *data;
  18.   node_tag *next;
  19.   node(){};
  20. }node;
  21.  
  22. class foo {
  23.   //private:  let it be public as we want to test whats in it!
  24. public:
  25.   node *head;
  26.  
  27. public:
  28.   foo(void);            // constructor
  29.   ~foo(void);           // destructor
  30.   void func(void *object, int object_size);
  31. }
  32.  
  33.  
  34. and in its .C file I have 
  35.  
  36. void foo::func(void *object, int object_size)
  37. {
  38.   node *new_node;
  39.   node *ptr;
  40.  
  41.   // create new_node
  42.   new_node = new node();
  43.   // allocate memory for object
  44.   if (NULL == (new_node->data = malloc(object_size))){
  45.     perror("malloc");
  46.     exit (EXIT_FAILURE);
  47.   }
  48.   // copy contents of object into data
  49.   if (NULL == memcpy(new_node->data,object,object_size)){
  50.     perror("memcpy");
  51.     exit (EXIT_FAILURE);
  52.   }
  53.  
  54.   // If this is the first node ...
  55.   if (head == NULL)
  56.     if (NULL == (head = new node())){
  57.       perror("new");
  58.       exit (EXIT_FAILURE);
  59.     }
  60.  
  61.   ptr = head;
  62.   while (ptr->next != NULL)
  63.     // warning: ANSI C++ forbids implicit conversion
  64.     // from `void *' in assignment.
  65.     // But it compiles fine.
  66.     ptr = (void*)(ptr->next);
  67.  
  68.   // append object
  69.   ptr->next = new_node;
  70. }
  71.  
  72.  
  73. Then in a another .C file that I'm using to test the stuff above I have ...
  74.  
  75.  
  76. typedef struct data_struct_tag{
  77.     int i;
  78.     char str[20];
  79. }data_struct;
  80.  
  81.  
  82. Now, some declarations
  83.  
  84. data_struct *t, *test_ptr;
  85. foo *f;
  86.  
  87. and allocate some memory storage for them ...
  88.  
  89. f = new foo();
  90.  
  91. if (NULL == (t = (data_struct*)malloc(sizeof(data_struct)))){
  92.     perror ("whatever");
  93.     exit (1);
  94. }
  95.  
  96. Now assign t some values ...
  97.  
  98. t->i = 256;
  99. strcpy(t->str,"this is a test");
  100.  
  101. have peek at what it's got ...
  102.  
  103. cout << t->str << " " << t->i "\n";
  104.  
  105. and yes it holds the assigned values.
  106.  
  107. But .....
  108.  
  109. when I do:
  110.  
  111. f->func(t,sizeof(data_struct));
  112.  
  113.   ptr = (data_struct*)(f->head->data);
  114.  
  115.   cout << ptr->str << "" << ptr->i << "\n";
  116.  
  117. I get goobly-gook nothing like "this is a test 256".
  118.  
  119. when I run the debuger (gdb ... via xxgdb) and peek at 
  120.  
  121. func(t,sizeof(data_struct));
  122.  
  123. i.e. when it is just entered into the first statement of 
  124.  
  125. void foo::func(void *object, int object_size)
  126.  
  127. I typecast object (which is void) to (data_struct*)object and have a peek
  128. at (data_struct*)object->str and (data_struct*)object->i and they
  129. have goobly-gook instead of the values that I asigned 't'.
  130.  
  131. Any one know what's going on? what point am I missing?
  132.  
  133. Any ideas greatly appreciated ... if you are going to "reply" please email
  134. me as well as "followup".
  135.  
  136. TIA
  137.  
  138.     Rob
  139.  
  140.  
  141.  
  142.  
  143.